﻿using App9.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using System.Text.RegularExpressions;

namespace App9.Models
{
    public class User : ModelBase<User>
    {
        public User()
        {
            // defaults 
            this.FirstName.Value = string.Empty;
            this.LastName.Value = string.Empty;
            this.AcceptTerms.Value = false;

            this.Validator = e =>
            {
                if (string.IsNullOrWhiteSpace(this.FirstName.Value))
                    this.FirstName.Errors.Add("First Name is required");
                else if (this.FirstName.Value.Length <= 3)
                    this.FirstName.Errors.Add("First Name must be greater than 3 chars");
                if (this.FirstName.Value.Equals("Jerry"))
                    this.FirstName.Errors.Add("First Name cannot be 'Jerry'");

                if (string.IsNullOrWhiteSpace(this.LastName.Value))
                    this.LastName.Errors.Add("Last Name is required");
                else if (this.LastName.Value.Length <= 3)
                    this.LastName.Errors.Add("Last Name must be greater than 3 chars");
                if (this.LastName.Value.Equals("Nixon"))
                    this.LastName.Errors.Add("Last Name cannot be 'Nixon'");

                this.Email.IsEnabled = this.ReceiveCommunications.Value;
                if (this.Email.IsEnabled && !new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$").Match(this.Email.Value).Success)
                    this.Email.Errors.Add("Email must be valid format");

                if (!this.AcceptTerms.Value)
                    this.AcceptTerms.Errors.Add("You must accept the terms");
            };
        }

        private Property<string> _FirstName = new Property<string>();
        public Property<string> FirstName { get { return _FirstName; } }

        private Property<string> _LastName = new Property<string>();
        public Property<string> LastName { get { return _LastName; } }

        private Property<bool> _ReceiveCommunications = new Property<bool>();
        public Property<bool> ReceiveCommunications { get { return _ReceiveCommunications; } }

        private Property<string> _Email = new Property<string>();
        public Property<string> Email { get { return _Email; } }

        private Property<bool> _AcceptTerms = new Property<bool>();
        public Property<bool> AcceptTerms { get { return _AcceptTerms; } }
    }
}
